package com.skcc.ts.nsi.project;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

import com.skcc.ts.nsi.R;
import com.skcc.ts.nsi.service.BaseExpandableAdapter;
import com.skcc.ts.nsi.service.MEDataHandler;

public class MEPjtMain extends Activity {

	private ArrayList<String[]> acntName = null;
	private ArrayList<String[]> acntTotal = null;
	private ArrayList<String[]> acntBalance = null;
	private ArrayList<String> pGroupList = null;
	private ArrayList<ArrayList<String>> pChildList = null;
	private ArrayList<String> pChildListContent = null;
	private String addr;
	private String port;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mepjtmain);

		setLayout(); // 레이아웃 세팅
		doListPjt(); // 그룹 & 차일드 구성

		pjtListView.setAdapter(new BaseExpandableAdapter(this, pGroupList, pChildList));

		// 그룹 클릭했을때 이벤트
		pjtListView.setOnGroupClickListener(new OnGroupClickListener() {

			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

				return false;

			}
		});

		// 차일드 클릭했을 때 이벤트
		pjtListView.setOnChildClickListener(new OnChildClickListener() {
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

				return false;

			}
		});

		// 그룹이 닫힐때 이벤트
		pjtListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			public void onGroupCollapse(int groupPosition) {

			}
		});

		// 그룹이 열릴때 이벤트
		pjtListView.setOnGroupExpandListener(new OnGroupExpandListener() {
			public void onGroupExpand(int groupPosition) {

			}

		});

	}

	private void doListPjt() {// 프로젝트정보 수신 => expandableListing
		Intent receiveI = getIntent();
		boolean doYN = receiveI.getBooleanExtra("start", false);
		if (doYN) {
			int count = receiveI.getIntExtra("count", 0); // number of projects
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");
			String[] pjt_code = receiveI.getStringArrayExtra("projectCode");
			String[] pjt_name = receiveI.getStringArrayExtra("projectName");
			String[] pjt_start = receiveI.getStringArrayExtra("projectStart");
			String[] pjt_end = receiveI.getStringArrayExtra("projectEnd");

			ArrayList<ArrayList<String>> aNameList = new ArrayList<ArrayList<String>>();
			ArrayList<ArrayList<String>> aTotalList = new ArrayList<ArrayList<String>>();
			ArrayList<ArrayList<String>> aBalanceList = new ArrayList<ArrayList<String>>();

			for (int i = 0; i < count; i++) {
				ArrayList<String> acnt_name = new ArrayList<String>();
				ArrayList<String> acnt_total = new ArrayList<String>();
				ArrayList<String> acnt_balance = new ArrayList<String>();

				String[] name = receiveI.getStringArrayExtra("acntVo:" + i + ":acntName");
				String[] total = receiveI.getStringArrayExtra("acntVo:" + i + ":acntTotal");
				String[] balance = receiveI.getStringArrayExtra("acntVo:" + i + ":acntBalance");
				for (int j = 0; j < name.length; j++) {
					acnt_name.add(name[j]);
					acnt_total.add(total[j]);
					acnt_balance.add(balance[j]);
				}
				aNameList.add(acnt_name);
				aTotalList.add(acnt_total);
				aBalanceList.add(acnt_balance);
			}
			String pjtGroup = ""; // 리스트 그룹만들기
			String pjtChild = "";
			pGroupList = new ArrayList<String>();
			pChildList = new ArrayList<ArrayList<String>>();
			for (int i = 0; i < count; i++) {
				pjtGroup = pjt_name[i].concat("\n(시작일 : ").concat(pjt_start[i]).concat(", 종료일 : ").concat(pjt_end[i]).concat(")");
				pGroupList.add(pjtGroup);
			}

			for (int i = 0; i < count; i++) {
				pChildListContent = new ArrayList<String>();
				for (int k = 0; k < aNameList.get(i).size(); k++) {

					pjtChild = "계정명 : " + (aNameList.get(i)).get(k) + "\n총액 : " + MEDataHandler.addComma((aTotalList.get(i)).get(k)) + "원"
							+ "\n잔액 : " + MEDataHandler.addComma((aBalanceList.get(i)).get(k)) + "원";

					pChildListContent.add(pjtChild);

				}
				pChildList.add(pChildListContent);// 리스트 차일딩
			}

		}
	}

	// 레이아웃 세팅
	private ExpandableListView pjtListView;

	private void setLayout() {
		pjtListView = (ExpandableListView) findViewById(R.id.expandableListView1);

	}
}